package handler

import (
	"DataSrv/data"
	"Gin/Api/request"
	"Gin/global/model"
	"Gin/global/pkg"
	context2 "context"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UploadVideo(context *gin.Context) {
	var VideoUploadRequest request.VideoUploadRequest
	if err := context.ShouldBind(&VideoUploadRequest); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	context.Request.ParseForm()
	file, m, err := context.Request.FormFile("video")
	if err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	upload, err := pkg.Upload(file, m)
	if err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	videoUpload, err := model.DataClient.VideoUpload(context2.Background(), &data.VideoUploadRequest{
		Url:       upload,
		UserId:    VideoUploadRequest.UserId,
		TypeId:    VideoUploadRequest.Type,
		Title:     VideoUploadRequest.Tiele,
		Des:       VideoUploadRequest.Des,
		Address:   VideoUploadRequest.Address,
		Tag:       VideoUploadRequest.Tag,
		ClassesId: VideoUploadRequest.ClassesId,
		Len:       VideoUploadRequest.Len,
		IsVip:     VideoUploadRequest.IsVip,
	})
	if err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"data":    videoUpload,
		"message": "上传成功",
	})
}
